<?php

class Ctrl_donatur extends CI_Controller {

	function __construct() {
		parent::__construct();
		if (!$this->session->userdata('adminin')) {
			redirect(base_url() . 'admin/home');
		}
		$this->load->model('Model_donatur');
		$this->load->model('Model_user');
		$this->load->model('Model_mahasiswa');
	}

	function index() {
		$this->tampil_donatur();
	}

	function tampil_donatur() {
		$data['row'] = $this->Model_donatur->select_all();
		$data['title'] = 'List Donatur';
		$this->load->view('admin/admin_header_view');
		$this->load->view('admin/list_donatur', $data);
		$this->load->view('admin/admin_footer_view');
	}

	function delete_donatur($id) {
		$this->db->delete('donatur', array('iddonatur' => $id));
		$this->db->delete('donasi', array('iddonatur' => $id));
		redirect(base_url() . 'ctrl_donatur');
	}

	function edit_donatur($id) {
		//mengedit donatur
		$donatur = $this->Model_donatur->selectbyid($id);
		$display['data'] = $donatur[0];
		$this->load->view('admin/admin_header_view');
		$this->load->view('admin/form_edit', $display);
		$this->load->view('admin/admin_footer_view');
	}

	function update_donatur() {
		$username = $this->input->post('nama');
		if ($username != '') {
			$where['iddonatur'] = $this->input->post('iddonatur');
			$this->Model_donatur->update($_POST, $where);
			redirect(base_url()."admin/ctrl_donatur/profil_donatur/".$where['iddonatur']);
		} else {
			$content['pesan'] = 'format edit salah, <a href="javascript:back()">kembali</a>';
			$data['title'] = 'Pesan error';
			$this->load->view('admin/admin_header_view', $data);
			$this->load->view('admin/hal_error', $content);
			$this->load->view('admin/admin_footer_view');
		}
	}

	function profil_donatur($id) {
		$array = array('iddonatur' => $id);
		$row = $this->Model_donatur->select($array);
		$data['row'] = $row[0];
		$data['title'] = 'Profil Donatur';
		$data['role'] = $this->session->userdata('role');
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/profil_donatur', $data);
		$this->load->view('admin/admin_footer_view');
	}

	function change_status($status, $id) {
		$username = $this->session->userdata('username');
		if ($row == $this->Model_donatur->selectbyid($id)) {
			$tujuan = $row[0]['email'];
			if ($status == 'Approved') {
				$rand_username = str_replace(" ", ".", $row[0]['nama']);
				$rand_password = $this->randomString(8);
				$isi = "username : " . $rand_username . "\npassword : " . $rand_password;
				$ganti = array('username' => $rand_username, 'password' => md5($rand_password), 'status' => $status);
			} else {
				$ganti = array('status' => $status);
			}
			$where = array('iddonatur' => $id);
			$this->Model_donatur->update($ganti, $where);
			//die($status);
			if ($status == 'Approved')
				$this->kirim_mail($tujuan, "balasan beasiswa", "anda sudah menjadi donatur");
			else if ($status == 'Rejected') {
				$isi = "maaf anda ditolak";
				$this->kirim_mail($tujuan, "balasan beasiswa", $isi);
			} else if ($status == 'Notified') {
				$isi = "silahkan kirim uang anda ke rekening bni itb";
//                die($tujuan);
				$this->kirim_mail($tujuan, "balasan beasiswa", $isi);
			}
			$this->profil_donatur($id);
		}
	}

	
	function kirim_mail($tujuan, $subject, $isi) {
		$this->load->library('email');

		$config['protocol'] = 'smtp';
		$config['smtp_host'] = 'mx2.itb.ac.id';
		$config['smtp_port'] = 25;

		$config['charset'] = 'iso-8859-1';
		$config['wordwrap'] = TRUE;
		$this->email->initialize($config);

		$this->email->from('muhammad.fazri@comlabs.itb.ac.id', 'Admin');
		$this->email->to($tujuan);

		$this->email->subject($subject);
		$this->email->message($isi);

		$this->email->send();
	}

	function tambah_donatur() {
		$data['title'] = 'Tambah Donatur';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/tambah_donatur');
		$this->load->view('admin/admin_footer_view');
	}

	function insert_donatur() {
		$_POST['password'] = md5($_POST['password']);
		$this->db->insert('donatur', $_POST);
		redirect(base_url() . 'admin/ctrl_donatur');
	}

	private function randomString($len) {
		return random_string('alnum', $len);
	}

	function histori_tambahan_donasi($id) {
		$data['title'] = 'Histori Tambahan Donasi';
		$data['row'] = $this->Model_donatur->select_tambah_donasi_by_id_donatur($id);
                $data['type'] = '';
		//$this->load->view('admin/admin_header_view');
		$this->load->view('admin/histori_tambahan_donasi', $data);
		$this->load->view('admin/admin_footer_view');
	}
	
	function list_histori_tambahan_donasi(){
		$data['title'] = 'Histori Tambahan Donasi';
		$data['row'] = $this->Model_donatur->select_tambah_donasi();
		$data['type'] = 'all';
		$this->load->view('admin/admin_header_view');
		$this->load->view('admin/histori_tambahan_donasi',$data);
		$this->load->view('admin/admin_footer_view');
    }

	function approve_tambah_donasi($id,$type){
		$data['row'] = $this->Model_donatur->select_tambah_donasi_by_id($id);
		$iddonatur = $data['row'][0]['iddonatur'];
		$this->Model_donatur->edit_tambah_donasi($id,array('status'=>'Approved'));
		$this->Model_donatur->tambah_donasi_donatur($iddonatur,$data['row'][0]['jumlah_tambahan']);
		$select = $this->Model_donatur->select(array('iddonatur'=>$iddonatur));
		$email = $select[0]['email'];
		$tujuan2 = $email;
		$subject2 = "beasiswa itb";
		$isi2 = "tambahan donasi anda sebesar ".$data['row'][0]['jumlah_tambahan']." yang dikirim pada tanggal ". $data['row'][0]['tanggal'] ." sudah diapprove";
		$this->kirim_mail($tujuan2, $subject2, $isi2);
		if($type=='all')
			redirect(base_url().'admin/ctrl_donatur/list_histori_tambahan_donasi');
		redirect(base_url().'admin/ctrl_donatur/histori_tambahan_donasi/'.$data['row'][0]['iddonatur']);
    }

	function reject_tambah_donasi($id,$types){
            $data['row'] = $this->Model_donatur->select_tambah_donasi_by_id($id);
            $iddonatur = $data['row'][0]['iddonatur'];
            $this->Model_donatur->edit_tambah_donasi($id,array('status'=>'Rejected'));
            $select = $this->Model_donatur->select(array('iddonatur'=>$iddonatur));
            $email = $select[0]['email'];
            $tujuan2 = $email;
            $subject2 = "beasiswa itb";
            $isi2 = "tambahan donasi anda sebesar ".$data['row'][0]['jumlah_tambahan']." yang dikirim pada tanggal ". $data['row'][0]['tanggal'] ." ditolak";
            $this->kirim_mail($tujuan2, $subject2, $isi2);
            if($type=='all')
                redirect(base_url().'admin/ctrl_donatur/list_histori_tambahan_donasi');
            redirect(base_url().'admin/ctrl_donatur/histori_tambahan_donasi/'.$data['row'][0]['iddonatur']);
    }
	
	
	function cancel_approve_tambah_donasi($id,$type){
            $data['row'] = $this->Model_donatur->select_tambah_donasi_by_id($id);
            $iddonatur = $data['row'][0]['iddonatur'];
            $this->Model_donatur->edit_tambah_donasi($id,array('status'=>'Requested'));
            $this->Model_donatur->tambah_donasi_donatur($iddonatur,(0-$data['row'][0]['jumlah_tambahan']));
            $select = $this->Model_donatur->select(array('iddonatur'=>$iddonatur));
            $email = $select[0]['email'];
            $tujuan2 = $email;
            $subject2 = "beasiswa itb";
            $isi2 = "tambahan donasi anda sebesar ".$data['row'][0]['jumlah_tambahan']." yang dikirim pada tanggal ". $data['row'][0]['tanggal'] ." tidak jadi diapprove";
            $this->kirim_mail($tujuan2, $subject2, $isi2);
            if($type=='all')
                redirect(base_url().'admin/ctrl_donatur/list_histori_tambahan_donasi');
            redirect(base_url().'admin/ctrl_donatur/histori_tambahan_donasi/'.$data['row'][0]['iddonatur']);
    }
	
	function cancel_rejected_tambah_donasi($id,$type){
		$data['row'] = $this->Model_donatur->select_tambah_donasi_by_id($id);
		$iddonatur = $data['row'][0]['iddonatur'];
		$this->Model_donatur->edit_tambah_donasi($id,array('status'=>'Requested'));
		$select = $this->Model_donatur->select(array('iddonatur'=>$iddonatur));
		$email = $select[0]['email'];
		$tujuan2 = $email;
		$subject2 = "beasiswa itb";
		$isi2 = "tambahan donasi anda sebesar ".$data['row'][0]['jumlah_tambahan']." yang dikirim pada tanggal ". $data['row'][0]['tanggal'] ." tidak jadi ditolak";
		$this->kirim_mail($tujuan2, $subject2, $isi2);
		if($type=='all')
			redirect(base_url().'admin/ctrl_donatur/list_histori_tambahan_donasi');
		redirect(base_url().'admin/ctrl_donatur/histori_tambahan_donasi/'.$data['row'][0]['iddonatur']);
    }
	
}